package com.ysu.zyw.controller;

import com.ysu.zyw.other.exception.KnownException;
import com.ysu.zyw.service.BlogPromptService;
import com.ysu.zyw.service.BlogUploadService;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by zhang_yao_wu1993 on 15/4/27.
 *
 * @author by Zerur
 */
@Controller
public class BlogUploadController {
    private BlogPromptService blogPromptService;
    private BlogUploadService blogUploadService;

    public BlogUploadService getBlogUploadService() {
        return blogUploadService;
    }

    @Resource(name = "blogUploadService")
    public void setBlogUploadService(BlogUploadService blogUploadService) {
        this.blogUploadService = blogUploadService;
    }

    public BlogPromptService getBlogPromptService() {
        return blogPromptService;
    }

    @Resource(name = "blogPromptService")
    public void setBlogPromptService(BlogPromptService blogPromptService) {
        this.blogPromptService = blogPromptService;
    }


    @RequestMapping("/AddCategory")
    public String addCategory(String category) {
        blogUploadService.addCategory(category);
        return "redirect:UploadPage";
    }

    @RequestMapping("/AddArticle")
    public String addArticle(String title, String categoryId, MultipartFile file, HttpServletRequest request) throws Exception {
        blogUploadService.addArticle(title, categoryId, file, request);
        return "redirect:SuccessPromptPage";
    }



    @ExceptionHandler(AccessDeniedException.class)
    public ModelAndView handlerAccessDeniedException(AccessDeniedException e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillInsufficientPermissionsModelAndView(modelAndView);
        return modelAndView;
    }

    @ExceptionHandler(KnownException.class)
    public ModelAndView handlerKnownException(KnownException e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillKnownExceptionPromptModelAndView(modelAndView, e);
        return modelAndView;
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView handlerUnknownException(Exception e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillUnknownExceptionPromptModelAndView(modelAndView);
        return modelAndView;
    }
}
